/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.withone.osgi.gadget.bigmouse.base;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import net.miginfocom.swing.MigLayout;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

/**
 * ログイン画面制御用クラス
 * 
 * @author Sasaki Katsuyuki
 *
 */
public class MouseLogin extends JFrame implements ActionListener{

	JFrame frame;
	Container contentPane;

	JLabel labelName;
	JLabel labelPass;
	JTextField txtName;
	JPasswordField pass;
	JButton btnOk;
	
	JDialog dialog;

	public static Dimension dimension;
	

	/**
	 * コンストラクタ
	 * 
	 */
	public MouseLogin() {

		frame = new JFrame("Login");
		contentPane = frame.getContentPane();
		MouseCommon.listInit();
		MouseCommon.listFrames.add(frame);
	}
	
	
	/**
	 * 起動用メソッド
	 * 
	 */
	public void run() {

		JPanel panelLogin = new JPanel(new MigLayout());
		
		labelName = new JLabel("ユーザ名");
		labelPass = new JLabel("パスワード");
		
		txtName = new JTextField(30);
		
		pass = new JPasswordField(30);
		
		btnOk = new JButton("OK");
		btnOk.addActionListener(this);
		
		panelLogin.add(labelName,"wrap");
		panelLogin.add(txtName,"wrap");
		panelLogin.add(labelPass,"wrap");
		panelLogin.add(pass,"wrap");
		panelLogin.add(btnOk,"center");
		
		contentPane.add(panelLogin);
		
		frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		frame.setSize(310,150);
//		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	/**
	 * OKボタン押下時の処理
	 * 
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()==btnOk){
			
			String name = txtName.getText();
			String password = new String(pass.getPassword());
			
			Twitter twitter = new TwitterFactory().getInstance(name, password);
			
			try {
				twitter.verifyCredentials();
				MouseMain main = new MouseMain(twitter);
				frame.dispose();
				
			} catch (TwitterException e1) {
				
				System.out.println("失敗");
				
				dialog = new JDialog(this,"Caution",true);
				
				JLabel diaLabel = new JLabel("<html>ユーザ・パスワードが違うか、<br/>ネットワーク障害のため<br/>ログインできません!!</html>", SwingConstants.CENTER);
				dialog.getContentPane().add(diaLabel);

				dialog.setSize(300,300);
				dialog.setResizable(false);
				dialog.setLocationRelativeTo(this);
				dialog.show();
			}
		}
	}
	
}
